package com.zhu.stack.impl;

import com.zhu.linkedlist.MyLinkedList;
import com.zhu.stack.Stack;

/**
 * 基于链表的栈(链表头是栈顶）
 * @author: zhu
 * @date: 2019/1/13 14:43
 */
public class LinkedListStack<E> implements Stack<E> {

    private MyLinkedList<E> list;//自己实现的链表
    public LinkedListStack(){
        list = new MyLinkedList<>();
    }

    @Override
    public int getSize() {
        return list.getSize();
    }

    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }

    @Override
    public void push(E e) {
        list.addFirst(e);
    }

    @Override
    public E pop() {
        return list.removeFirst();
    }

    @Override
    public E peek() {
        return list.getFirst();
    }

    @Override
    public String toString(){
        StringBuilder res = new StringBuilder();
        res.append("Stack: top");
        res.append(list);
        return res.toString();
    }
}
